from influxdb import InfluxDBClient
import os


# 查到的数据写入文件
def writeDatas(client,sql,filename):
    document = open(filename, 'w')
    result = client.query(sql)
    document.write('Result:{0}'.format(result))
    document.close()

# 取出数据，用于备用
def getBytesData(file):
    defaultFile = open(file)
    for line in defaultFile:
        parts = line.split("[")[1].split("]")[0]
        # print('数据流内容：',parts)
    return parts

# 重新将优化格式的数据写入文件
def split_data(data,file_name):
    list_byte = data.split('},')
    file = open(file_name,'w+')
    for line in list_byte:
        file.write(line+'\n')


# 东西流量文件
port_file = 'port_bytes.txt'
new_port_file = 'new_port_bytes.txt'

# 南北流量文件
fip_file = 'fip_bytes'
new_fip_file = 'new_fip_bytes.txt'



bytes_file = port_file
new_bytes_file = new_port_file

client = InfluxDBClient('10.131.48.200',8086,'root','huayun2017','stackwatch')
sql = "select * from port_bytes.txt where direction = 'o' and dip = '10.10.10.7'"
sql = "select value from port_bytes.txt where direction = 'o' and dip = '10.10.10.7'"

# writeDatas(client,sql,bytes_file)
bytes_data = getBytesData(bytes_file)

# split_data(bytes_data,new_bytes_file)



